package cn.tedu.javagame.shoot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.util.List;
import java.util.ArrayList;


public class ShootPanel extends JPanel implements MouseMotionListener{
    Image bgImage = new ImageIcon("plane/background.jpg").getImage();
    int bgY = 0;
    Hero hero = new Hero();
    List<FlyObject> flysObjects = new ArrayList<>();
    List<Bullet> bullets = new ArrayList<>();
    public ShootPanel(){
        this.addMouseMotionListener(this);
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        paintBackground(g);
        paintHero(g);
        paintScore(g);
        paintBullet(g);
        paintFlyObject(g);
    }
    Image gg = new ImageIcon("plane/gg.png").getImage();
    //绘制背景
    public void paintBackground(Graphics g){
        g.drawImage(bgImage, 0, bgY, null);
        g.drawImage(bgImage, 0, bgY-ShootFrame.HEIGHT, null);
    }
    //绘制英雄机
    public void paintHero(Graphics g){
        g.drawImage(hero.heroImg, hero.x, hero.y, null);
    }
    //绘制ui
    public void paintScore(Graphics g){
        g.drawImage(gg,5,660,null);
        g.setColor(Color.RED);
        Font f = new Font("宋体", Font.BOLD, 28);
        g.setFont(f);
        g.drawString("得分："+hero.score,350,710);
        g.drawString("金币："+hero.gold,230,710);
        g.drawString("生命："+hero.life,80,710);
        g.setColor(Color.GREEN);
        g.drawString("关卡："+(hero.score/1000+1),370,30);
    }
    public void action() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                bgMove();
                hero.step();
                bulletAction();//产生子弹
                bulletMove();
                bulletOutOfBounds();//子弹越界处理
                flysObjectAction();
                flyObjectMoveAndStep();
                hitAction();
                repaint();
            }
        },0,10);
    }
    //产生飞行物
    int index = -1;
    public void flysObjectAction(){
        index++;
        if(index==Integer.MAX_VALUE){index=-1;}
        if(index%20==0){
            FlyObject flyObject = new Enemy();
            flysObjects.add(flyObject);
        }
        if(index%30==0){
            FlyObject flyObject = new Heart();
            flysObjects.add(flyObject);
        }
    }
    public void paintFlyObject(Graphics g){
        for(int i = 0; i< flysObjects.size(); i++){
            FlyObject flyObject = flysObjects.get(i);
            g.drawImage(flyObject.img,flyObject.x,flyObject.y,null);
        }
    }
    public void flyObjectMoveAndStep(){
        for(int i = 0; i< flysObjects.size(); i++){
            FlyObject flyObject = flysObjects.get(i);
            flyObject.move();
            flyObject.step();
        }
    }
    int bulletIndex = -1;
    public void bulletAction(){
        bulletIndex++;
        if(bulletIndex == Integer.MAX_VALUE){bulletIndex = 0;}
        if(bulletIndex%25==0){bullets.addAll(hero.shoot());}
    }
    public void paintBullet(Graphics g){
        for(int i=0;i<bullets.size();i++){
            Bullet bullet = bullets.get(i);
            g.drawImage(Bullet.bulletImg,bullet.x,bullet.y,null);
        }
    }
    public void bulletMove(){
        for(int i=0;i<bullets.size();i++){
            Bullet bullet = bullets.get(i);
            bullet.move();
        }
    }
    public void bulletOutOfBounds(){
        for(int i=0;i<bullets.size();i++){
            Bullet bullet = bullets.get(i);
            if(bullet.y<0){bullets.remove(i);}
        }
    }
    public void hitAction(){
        for(int i=0;i<flysObjects.size();i++){
            FlyObject flyObject = flysObjects.get(i);
            //英雄机 敌机
            //英雄机 小红心
            //子弹  敌机
            if(flyObject instanceof Heart){//instanceof判断对象是不是某一个类型的对象
                Heart heart = (Heart)flyObject;
                hero.hitHeart(heart);
            }
            if(flyObject instanceof Enemy){
                Enemy enemy = (Enemy)(flyObject);
                hero.hitEnemy(enemy);
                for(int j=0;j<bullets.size();j++){
                    Bullet bullet = bullets.get(j);
                    bullet.hitEnemy(enemy);
                }
            }

        }
    }
    public void bgMove(){
        bgY ++;
        if(bgY >= ShootFrame.HEIGHT){bgY=0;}
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        hero.move(e.getX(), e.getY());
    }
}
